
load('summary_data/tableA6_A7_A8_A9.rda')

make_table = function(data, title){
  gt(data) %>%
    tab_header(title = title) |>
    fmt_percent(columns = Obama12:Trump20, decimals = 1) |>
    cols_label(
      Demographics = "",
      Obama12 = "Obama '12",
      Romney12 = "Romney '12",
      Clinton16 = "Clinton '16",
      Trump16 = "Trump '16",
      Biden20 = "Biden '20",
      Trump20 = "Trump '20"
    ) |> 
    tab_row_group(label = "Education Within White Donors", rows = 10:11)|>
    tab_row_group(label = "Education", rows = 8:9)|>
    tab_row_group(label = "Ethnorace", rows = 3:7)|>
    tab_row_group(label = "Gender", rows = 1:2)|>
    as_latex()
}

make_table(
  dcast(out[var == 'n' & bottom90 == T, -'value'], 'Demographics ~ cand', value.var = 'percs')[,c(1,4:5,3,6,2,7)],
  'Matched Donor Number - Bottom 90%'
  ) %>%
  as.character() %>%
  write_lines('tables/tableA6.tex')

make_table(
  dcast(out[var == 'val' & bottom90 == T, -'value'], 'Demographics ~ cand', value.var = 'percs')[,c(1,4:5,3,6,2,7)],
  'Matched Donor Amounts - Bottom 90%'
) %>%
  as.character() %>%
  write_lines('tables/tableA7.tex')

make_table(
  dcast(out[var == 'n' & bottom90 == F, -'value'], 'Demographics ~ cand', value.var = 'percs')[,c(1,4:5,3,6,2,7)],
  'Matched Donor Number - Top 10%'
) %>%
  as.character() %>%
  write_lines('tables/tableA8.tex')

make_table(
  dcast(out[var == 'val' & bottom90 == F, -'value'], 'Demographics ~ cand', value.var = 'percs')[,c(1,4:5,3,6,2,7)],
  'Matched Donor Amounts - Top 10%'
) %>%
  as.character() %>%
  write_lines('tables/tableA9.tex')

